package com.doit.spark.day04.demo

import com.doit.spark.day01.utils.SparkUtil
import org.apache.spark.SparkContext
import org.apache.spark.rdd.JdbcRDD

import java.sql.{DriverManager, ResultSet}

/**
 * @DATE 2022/1/6/15:15
 * @Author MDK
 * @Version 2021.2.2
 * */
object C01_JdbcRDD {
  def main(args: Array[String]): Unit = {
    val sc: SparkContext = SparkUtil.getSc
    // 获取连接
    val conn = ()=>DriverManager.getConnection("jdbc:mysql://linux01:3306/doit", "root", "root")
    // 执行SQL语句  表要有一个主键  自增
    val sql = "select * from tb_user where id >= ?  and id <= ?"
    // 封装结果
    val row = (rs:ResultSet)=>{
      val uid: String = rs.getString("uid")
      val name: String = rs.getString("name")
      (uid,name)
    }
    val mysqlTableRDD = new JdbcRDD[(String,String)](sc ,conn ,sql,1,3 ,2 ,row)
     mysqlTableRDD.foreach(println)

    //使用JDBCRDD时,  要有"范围字段"(lowerbound  upperbound)
  }
}
